import Vue from 'vue'
import Vuex from 'vuex'
import {getMusicDetail} from "../api/baidu-music";

Vue.use(Vuex)
const store = new Vuex.Store({
    // state， 存放的是vuex的数据
    state: {
        msg: "hello vuex",
        title: "标题",
        list: [
            1, 3, 4, 5, 6
        ],
        songinfo: {},
        bitrate:{},
        currentTime:0,
        //播放进度
        processTime:0,
        imgStyle:{
            'animation-play-state':'paused'
        },
        isPlay:true
    },
    // 所有涉及到state的变化都要通过mutation去修改
    // mutation是一个纯函数，里面不能参杂异步操作，不能涉及到时间操作
    mutations: {
        // 定义了一个名为changeTitle的mutation
        // state 当前的state
        // payload 调用mutation的时候传递的数据
        changeTitle(state, payload) {
            state.title = payload.title
        },
        changeSongInfo(state, payload) {
            state.songinfo = payload.songinfo
            state.bitrate = payload.bitrate
        },
        changeCurrentTime(state, payload){
            state.currentTime = payload.currentTime
        },
        changeProcessTime(state,payload){
            state.processTime = payload.processTime
        },
        changeImgStyle(state,payload){
            state.imgStyle = payload.imgStyle
        },
        changePlay(state,payload){
            state.isPlay = payload.isPlay
        }
    },
    getters: {
        num(state) {
            return state.list.length
        },
        searchList(state) {
            return state.list.filter(item => item > 3)
        }
    },
    // action里可以封装异步操作
    actions: {
        // 通过这个action初始化songinfo
        // initSongInfo(context, payload) {
        initSongInfo({commit}, payload) {
            // 请求接口
            // console.log(context)
            getMusicDetail(payload.songId).then(res => {
                // 拿到请求的结果
                // context.commit("changeSongInfo", {bitrate: res.bitrate, songinfo: res.songinfo})
                commit("changeSongInfo", {bitrate: res.data.bitrate, songinfo: res.data.songinfo})
            })
        }
    }
})
export default store